package com.cmhi.base.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInstaller;
import android.os.Bundle;

import com.cmhi.base.utils.BimTools;
import com.softmbh.kellyplatform.util.AppUtils;

public class Android_P_silenceInstallReceiver extends BroadcastReceiver {
    public static final String PACKAGE_INSTALLED_ACTION =
            "android.intent.action.PACKAGE_INSTAll";
    public static final String PACKAGE_UNINSTALLED_ACTION =
            "android.intent.action.PACKAGE_UN_INSTAll";
    public static final String RECEIVER_ACTION_SILENCE_INSTALL_START =
            "com.android.SilenceInstall.Start";

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle extras = intent.getExtras();
        AppUtils.debugLog(intent.toString());
        AppUtils.debugLog(intent.getAction());
        if(RECEIVER_ACTION_SILENCE_INSTALL_START.equals(intent.getAction())){

        }else if (PACKAGE_INSTALLED_ACTION.equals(intent.getAction())) {
            String apkPath = intent.getStringExtra("ApkPath");
            AppUtils.debugLog(intent.getAction() + apkPath);
            int status = extras.getInt(PackageInstaller.EXTRA_STATUS);
            String message = extras.getString(PackageInstaller.EXTRA_STATUS_MESSAGE);
            switch (status) {
                case PackageInstaller.STATUS_PENDING_USER_ACTION:
                    // This test app isn't privileged, so the user has to confirm the install.
                    Intent confirmIntent = (Intent) extras.get(Intent.EXTRA_INTENT);
//                    startActivity(confirmIntent);
                    break;

                case PackageInstaller.STATUS_SUCCESS:
                    AppUtils.debugLog("Install succeeded!" + apkPath);
                    silenceInstallResult("Success",apkPath);
                    break;

                case PackageInstaller.STATUS_FAILURE:
                case PackageInstaller.STATUS_FAILURE_ABORTED:
                case PackageInstaller.STATUS_FAILURE_BLOCKED:
                case PackageInstaller.STATUS_FAILURE_CONFLICT:
                case PackageInstaller.STATUS_FAILURE_INCOMPATIBLE:
                case PackageInstaller.STATUS_FAILURE_INVALID:
                case PackageInstaller.STATUS_FAILURE_STORAGE:
                    AppUtils.debugLog("Install failed! " + status + ", " + message);
                    silenceInstallResult("Error",apkPath);
                    break;
                default:
                    AppUtils.debugLog("Unrecognized status received from installer: " + status);
            }
        } else if (PACKAGE_UNINSTALLED_ACTION.equals(intent.getAction())) {
            String pkgName = intent.getStringExtra("PkgName");
            AppUtils.debugLog(intent.getAction() + pkgName);
            int status = extras.getInt(PackageInstaller.EXTRA_STATUS);
            String message = extras.getString(PackageInstaller.EXTRA_STATUS_MESSAGE);
            switch (status) {
                case PackageInstaller.STATUS_PENDING_USER_ACTION:
                    // This test app isn't privileged, so the user has to confirm the install.
                    Intent confirmIntent = (Intent) extras.get(Intent.EXTRA_INTENT);
//                    startActivity(confirmIntent);
                    break;

                case PackageInstaller.STATUS_SUCCESS:
                    AppUtils.debugLog("Uninstall succeeded!" + pkgName);
                    silenceUninstallSuccess("Success",pkgName);
                    break;

                case PackageInstaller.STATUS_FAILURE:
                case PackageInstaller.STATUS_FAILURE_ABORTED:
                case PackageInstaller.STATUS_FAILURE_BLOCKED:
                case PackageInstaller.STATUS_FAILURE_CONFLICT:
                case PackageInstaller.STATUS_FAILURE_INCOMPATIBLE:
                case PackageInstaller.STATUS_FAILURE_INVALID:
                case PackageInstaller.STATUS_FAILURE_STORAGE:
                    AppUtils.debugLog("Uninstall failed! " + status + ", " + message);
                    silenceUninstallSuccess("Error",pkgName);
                    break;
                default:
                    AppUtils.debugLog("Unrecognized status received from installer: " + status);
            }
        }
    }


    //静默安装成功
    private void silenceInstallResult(String result, String apkPath) {
        AppUtils.debugLog("silenceInstall result : " + result + ", apkPath : " + apkPath);
        Intent intent = new Intent("com.android.SilenceInstall.Over");
        intent.setPackage("com.aspirecn.combo.soft");
        intent.putExtra("Result", result);
        intent.putExtra("ApkPath", apkPath);
        intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        BimTools.getAppContext().sendBroadcast(intent);
        AppUtils.debugLog("sendBroadcast  silenceInstallResult SilenceInstall.Over result="+result);
    }

    //静默卸载成功
    private void silenceUninstallSuccess(String result,String PkgName) {
        AppUtils.debugLog("silenceUninstallSuccess PkgName : " + PkgName);
        Intent intent = new Intent("com.android.SilenceUninstall.Over");
        intent.setPackage("com.aspirecn.combo.soft");
        intent.putExtra("Result", result);
        intent.putExtra("PkgName", PkgName);
        intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        BimTools.getAppContext().sendBroadcast(intent);
        AppUtils.debugLog("sendBroadcast  silenceUninstallSuccess SilenceUninstall.Over result="+result);
    }
}